// pages/medetail/assess/assess.js
import {imguploadapi,assessapi} from "../../../utils/api"
import utils from "../../../utils/util"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        fileList: [],//页面展示图片数组
        imgarr:[],//上传后台的图片数组
        value:5,//评分
        order_id:"",//订单的id
        assess:{},//对应商品的数据
        content:"",//描述的内容
        window_heigt: '', //屏幕高
        window_width: '', //屏幕宽
        canv: {
          width: '', //canvas宽
          height: '', //canvas高
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(JSON.parse(options.obj),options.orderid);
        this.setData({
            assess:JSON.parse(options.obj),
            order_id:options.orderid
        })
    },
//    评分的选择
    onChange(event) {
      this.setData({
        value: event.detail,
      });
    },
    // 描述的函数
    contentfn(e){
        var addressreg = /^[^ ]/
        addressreg.test(e.detail.value)?utils.wxshowtoast("描述输入正确的"):utils.wxshowtoast("输入正确的描述")
        this.setData({
            content:e.detail.value
        })
    },
    //图片的选择
    _addImg() {
        var that = this;
        console.log(this.data.fileList.length)
        if (this.data.fileList.length==5) {
            return utils.wxshowtoast('最多5张图片')
        }
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
            sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
            success: res => {
            that.data.fileList= that.data.fileList.concat(res.tempFiles)
            that.setData({ 
            fileList: that.data.fileList
            });
            console.log(res.tempFilePaths);
            that.getCanvasImg(res.tempFilePaths)
            }
        })
      },
  //压缩并获取图片
  getCanvasImg:function (tempFilePaths) {
    var that = this
    wx.showLoading();
    wx.getImageInfo({
      src: tempFilePaths[0], //图片的路径，可以是相对路径，临时文件路径，存储文件路径，网络图片路径,  
      success: res => {
        console.log(res);
        that.data.canv.width = that.data.window_width;
        that.data.canv.height = that.data.window_width / res.width * res.height;
        that.setData({
          canv: that.data.canv
        })
        console.log(this.data.canv);
        const ctx = wx.createCanvasContext('attendCanvasId');
        setTimeout(() => {
          ctx.drawImage(tempFilePaths[0], 0, 0, that.data.canv.width, that.data.canv.height);
          ctx.draw(true, function () {
            wx.canvasToTempFilePath({
              canvasId: 'attendCanvasId',
              fileType:"jpg",
              success: function (res) {
               wx.hideLoading();
               that.uploadFile(res.tempFilePath)
              },
              fail: function (e) {
                that.getCanvasImg(tempFilePaths);
              }
            });
          });
        }, 1000);
      },
      fail: () => {},
      complete: () => {}
    });
  },
    async uploadFile(star){
    const res =await imguploadapi(star)
    wx.hideLoading()
    this.data.imgarr.push(JSON.parse(res.data).data.url)
    this.setData({
    imgarr:this.data.imgarr
    })
    },
      //点击进行图片删除
      _onDelTab(e) {
        // 获取图片索引
        let idx = e.currentTarget.dataset.idx;
        this.data.fileList.splice(idx, 1);
        this.data.imgarr.splice(idx, 1);
        this.setData({
          fileList: this.data.fileList,
          imgarr:this.data.imgarr
        })
      },
    //   评价提交
   async assessbtn(){
        var addressreg = /^[^ ]/
        if(this.data.content=="" || !addressreg.test(this.data.content)){
            return utils.wxshowtoast("输入正确的描述")
         }
        console.log(this.data.fileList,this.data.imgarr);
         const res =await assessapi({
            order_id:this.data.order_id,
            sku_id:this.data.assess.goods_sku_id,
            images:this.data.imgarr.join(","),
            user_score:this.data.value,
            content:this.data.content
         })
         console.log(res.data);
         wx.hideLoading()
         if(res.data.code===200){
              wx.redirectTo({
                url: '/pages/medetail/meorder/meorder?id=0'
              })
         }else{
             utils.wxshowtoast("提交失败")
         }
         
    },
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        var that = this;
        wx.getSystemInfo({
        success: function (res) {
            //获取屏幕窗口高度和宽度
            console.log(res);
            that.data.window_heigt = res.windowHeight
            that.data.window_width = res.windowWidth
        },
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})